#!/usr/bin/python

#This script is for model name in UserAgent.

import os
from xml.etree import ElementTree

#----------------------------------------------------
# Global Variables
#----------------------------------------------------
srcFile = 'ffos_wimdata_ng/wprocedures/isdm_UserAgent.plf'
DestFile = 'gecko/netwerk/protocol/http/jrdModelName.h'

# the SDMID
modelNameId = 'model_name_in_UA_string';

#----------------------------------------------------
# main
#----------------------------------------------------
def main():
    if not os.path.exists(srcFile):
        print 'no model name in UA string for customization.'
        return

    modelNameString = ''
    isModelNameIdFound = False

    fIn = open(srcFile, 'r')
    text = fIn.read()
    fIn.close()
    root = ElementTree.fromstring(text)
    varNodes = root.getiterator('VAR')
    for node in varNodes:
        sdmidTag = node.find('SIMPLE_VAR/SDMID')
        valueTag = node.find('SIMPLE_VAR/VALUE')
        if sdmidTag.text == modelNameId:
            isModelNameIdFound = True
            if valueTag.text == None:
                modelNameString = '""'
            else:
                modelNameString = '"%s"' % valueTag.text.strip('"')

    if isModelNameIdFound == False:
        print 'Error: Not found the SDMID: %s' % modelNameId
    else:
        fOut = open(DestFile, 'wb')
        hFileText = ''
        hFileText += '#ifndef jrdModelName_h__\n'
        hFileText += '#define jrdModelName_h__\n\n'
        hFileText += 'const char *sJrdModelName = %s;\n\n' % modelNameString
        hFileText += '#endif //jrdModelName_h__'
        
        fOut.write(hFileText)
        fOut.close()
        print 'generate %s succeeded' % DestFile



if __name__ == '__main__':
    main()